home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / mscheap2 / heap.h < prev    next >
C/C++ Source or Header  |  1990-03-12  |  8KB  |  353 lines

  1. /*/
  2.        Copyright (c) 1990 by Optimal Software, All Rights Reserved
  3. /*/
  4.  
  5. #if !defined( HEAP_H )
  6.  
  7. #define HEAP_H
  8.  
  9. #if   defined( M_I86SM )
  10.  
  11.    #define _HEAPLIB "sheap" 
  12.  
  13.    #define _DATA_NEAR
  14.    #define _CODE_NEAR
  15.  
  16. #elif defined( M_I86MM )
  17.  
  18.    #define _HEAPLIB "mheap" 
  19.  
  20.    #define _DATA_NEAR
  21.    #define _CODE_FAR
  22.  
  23. #elif defined( M_I86CM )
  24.  
  25.    #define _HEAPLIB "cheap" 
  26.  
  27.    #define _DATA_FAR
  28.    #define _CODE_NEAR
  29.  
  30. #elif defined( M_I86LM ) || defined( M_I86HM )
  31.  
  32.    #define _HEAPLIB "lheap" 
  33.  
  34.    #define _DATA_FAR
  35.    #define _CODE_FAR
  36.  
  37. #else
  38.  
  39.    #error Undefined address model
  40.  
  41. #endif
  42.  
  43. /*/
  44.       Tell linker to look for heap.lib
  45.  
  46.       N.b., this does not work, but MS might fix it some day.
  47. /*/
  48.  
  49. #if defined( MSC )
  50.    #pragma comment ( lib, _HEAPLIB )
  51. #endif
  52.  
  53. #if defined( _DATA_NEAR )
  54.  
  55.    #define _PREFIX( symbol ) _n##symbol
  56.  
  57. #elif defined( _DATA_FAR )
  58.  
  59.    #define _PREFIX( symbol ) _f##symbol
  60.  
  61. #endif
  62.  
  63. /*/
  64.    The following pre-processor symbols control the objective of the
  65.    heap manager.  The current setting is recorded in the _heapmode
  66.    variable.  Programs may change this value on the fly, but it is
  67.    usually best to set the desired mode at the beginning of the program
  68.    and leave it at that.
  69.  
  70.    Default: TIME   (because this approximates MSC usage)
  71. /*/
  72.  
  73. #if !defined( STDMSC )
  74.    #define _HEAPTIME  0
  75.    #define _HEAPSIZE  1
  76.    #define _HEAPSPACE 2
  77.  
  78.    #if !defined(_HEAPMODE)
  79.       #define _HEAPMODE _HEAPTIME
  80.    #endif
  81. #endif
  82.  
  83. /*/
  84.    Result code for _heapchk()
  85.  
  86.    c.f. MSC _heapchk(), _heapset(), _heapwalk()
  87. /*/
  88. #if !defined(_HEAPEMPTY)
  89.    #define _HEAPEMPTY      -1
  90.    #define _HEAPOK         -2
  91.    #define _HEAPBADBEGIN   -3
  92.    #define _HEAPBADNODE    -4    /* invalid control block */
  93.    #define _HEAPEND        -5
  94.    #define _HEAPBADPTR     -6    /* bad _heapwalk ptr or data corrupted */
  95.  
  96.    #define _FREEENTRY       0
  97.    #define _USEDENTRY       1
  98. #endif
  99.  
  100. #define SIZE_MAX 0xFFFF
  101.  
  102. /********** type definitions **********/
  103.  
  104. typedef unsigned int uint;
  105.  
  106. #if !defined(_SIZE_T_DEFINED)
  107.    typedef uint size_t;
  108.    #define _SIZE_T_DEFINED
  109. #endif
  110.  
  111. typedef struct    /* linkage in a doubly-linked list */
  112.    {
  113.    uint  last;    /* segment pointer to preceeding list entry */
  114.    uint  next;    /* segment pointer to following list entry */
  115.    } _LINK;
  116.  
  117. typedef struct    /* every arena obtained from DOS has one of these */    
  118.    {
  119.    _LINK link;    /* links to other arenas */
  120.    uint  size;    /* size of this arena including header and trailer */
  121.    uint  isdos;   /* memory from DOS?  [freeable by _heappack()] */
  122.    } _ARENA;
  123.  
  124. typedef struct    /* every allocated block has a header of this form */
  125.    {
  126.    _LINK clist;   /* links to other chunks in same arena */
  127.    _LINK flist;   /* links to free/join list -- 0 for an in-use entry */
  128.                   /* ... used by debugger to record info on used entries */
  129.    } _CHUNK;
  130.  
  131. #if !defined(_HEAPINFO_DEFINED)     /* c.f. MSC malloc.h */
  132.    typedef struct _heapinfo
  133.        {
  134.        int far *_pentry;
  135.        size_t   _size;
  136.        int      _useflag;
  137.        } _HEAPINFO;
  138.    #define _HEAPINFO_DEFINED
  139. #endif
  140.  
  141. /********** global variables; init in heaputil.c **********/
  142.  
  143. extern size_t cdecl _amblksiz;   /* DOS allocation quantum */
  144.  
  145. #if !defined( STDMSC )
  146.  
  147. extern int    cdecl _heapmode;   /* mode of operation; see TIME/SIZE/SPACE above */
  148.  
  149. extern size_t cdecl _heappad ;   /* padding at ends of every allocation */
  150.  
  151. #endif
  152.  
  153. /********** Function prototypes and aliases **********/
  154.  
  155. extern void      *cdecl alloca( size_t nbytes );
  156.  
  157. extern void      *cdecl calloc( size_t nitems, size_t nbytes );
  158.  
  159. extern void      *cdecl _expand( void *ptr, size_t nbytes );
  160.  
  161. #if !defined( STDMSC )
  162. extern void far  *cdecl _fcalloc( size_t nitems, size_t nbytes );
  163. #endif
  164.  
  165. #if !defined( STDMSC )
  166. extern void far  *cdecl _fexpand( void far *ptr, size_t nbytes );
  167. #endif
  168.  
  169. extern void       cdecl _ffree( void far *ptr );
  170.  
  171. extern int        cdecl _fheapchk( void );
  172.  
  173. #if !defined( STDMSC ) && defined( FILE )
  174. extern void       cdecl _fheapdump( FILE *output, int verbose );
  175. #endif
  176.  
  177. #if !defined( STDMSC )
  178. extern int        cdecl _fheappack( void );
  179. #endif
  180.  
  181. extern int        cdecl _fheapset( uint fill );
  182.  
  183. extern int        cdecl _fheapwalk( struct _heapinfo *hp );
  184.  
  185. #if !defined( STDMSC )
  186. extern int        _fheapwatch( void far *ptr, int enable );
  187. #endif
  188.  
  189. extern void far  *cdecl _fmalloc( size_t nbytes );
  190.  
  191. extern size_t     cdecl _fmsize( void far *ptr );
  192.  
  193. #if !defined( STDMSC )
  194. extern void far  *cdecl _frealloc( void far *ptr, size_t nbytes );
  195. #endif
  196.  
  197. extern void       cdecl free( void *ptr );
  198.  
  199. extern uint       cdecl _freect( size_t nbytes );
  200.  
  201. #if !defined( STDMSC )
  202. extern void far  *cdecl _frelocate( void far *ptr );
  203. #endif
  204.  
  205. #if defined( STDMSC )
  206. extern void huge *cdecl halloc( long nitems, size_t nbytes );
  207. #endif
  208.  
  209. #if !defined( STDMSC )
  210. extern void huge *cdecl _hcalloc( long nitems, size_t nbytes );
  211. #endif
  212.  
  213. extern int        cdecl _heapchk( void );
  214.  
  215. #if !defined( STDMSC ) && defined( FILE )
  216. extern void       cdecl _heapdump( FILE *output, int verbose );
  217. #endif
  218.  
  219. #if !defined( STDMSC )
  220. extern int        cdecl _heappack( void );
  221. #endif
  222.  
  223. extern int        cdecl _heapset( uint fill );
  224.  
  225. #if !defined( STDMSC )
  226. extern char      *cdecl _heapstat( int status );
  227. #endif
  228.  
  229. extern int        cdecl _heapwalk( struct _heapinfo *hp );
  230.  
  231. #if !defined( STDMSC )
  232. extern int        cdecl _heapwatch( void *ptr, int enable );
  233. #endif
  234.  
  235. #if !defined( STDMSC )
  236. extern void huge *cdecl _hexpand( void huge *ptr, long nbytes );
  237. #endif
  238.  
  239. #if defined( STDMSC )
  240. extern void       cdecl hfree( void huge *ptr );
  241. #endif
  242.  
  243. #if !defined( STDMSC )
  244. extern void       cdecl _hfree( void huge *ptr );
  245. #endif
  246.  
  247. extern int        cdecl _hheapchk( void );
  248.  
  249. #if !defined( STDMSC ) && defined( FILE )
  250. extern void       cdecl _hheapdump( FILE *output, int verbose );
  251. #endif
  252.  
  253. #if !defined( STDMSC )
  254. extern int        cdecl _hheappack( void );
  255. #endif
  256.  
  257. #if !defined( STDMSC )
  258. extern int        cdecl _hheapset( uint fill );
  259. #endif
  260.  
  261. #if !defined( STDMSC )
  262. extern int        cdecl _hheapwalk( struct _heapinfo *hp );
  263. #endif
  264.  
  265. #if !defined( STDMSC )
  266. extern int        cdecl _hheapwatch( void huge *ptr, int enable );
  267. #endif
  268.  
  269. #if !defined( STDMSC )
  270. extern void huge *cdecl _hmalloc( long nbytes );
  271. #endif
  272.  
  273. #if !defined( STDMSC )
  274. extern long       cdecl _hmsize( void huge *ptr );
  275. #endif
  276.  
  277. #if !defined( STDMSC )
  278. extern void huge *cdecl _hrealloc( void huge *ptr, long nbytes );
  279. #endif
  280.  
  281. #if !defined( STDMSC )
  282. extern void huge *cdecl _hrelocate( void huge *ptr );
  283. #endif
  284.  
  285. extern void      *cdecl malloc( size_t nbytes );
  286.  
  287. extern size_t     cdecl _memavl( void );
  288.  
  289. extern size_t     cdecl _memmax( void );
  290.  
  291. extern size_t     cdecl _msize( void *ptr );
  292.  
  293. #if !defined( STDMSC )
  294. extern void near *cdecl _ncalloc( size_t nitems, size_t nbytes );
  295. #endif
  296.  
  297. #if !defined( STDMSC )
  298. extern void near *cdecl _nexpand( void near *ptr, size_t nbytes );
  299. #endif
  300.  
  301. extern void       cdecl _nfree( void near *ptr );
  302.  
  303. extern int        cdecl _nheapchk( void );
  304.  
  305. #if !defined( STDMSC ) && defined( FILE )
  306. extern void       cdecl _nheapdump( FILE *output, int verbose );
  307. #endif
  308.  
  309. #if !defined( STDMSC )
  310. extern int        cdecl _nheappack( void );
  311. #endif
  312.  
  313. extern int        cdecl _nheapset( uint fill );
  314.  
  315. extern int        cdecl _nheapwalk( struct _heapinfo *hp );
  316.  
  317. #if !defined( STDMSC )
  318. extern int        cdecl _nheapwatch( void near *ptr, int enable );
  319. #endif
  320.  
  321. extern void near *cdecl _nmalloc( size_t nbytes );
  322.  
  323. extern size_t     cdecl _nmsize( void near *ptr );
  324.  
  325. #if !defined( STDMSC )
  326. extern void near *cdecl _nrealloc( void near *ptr, size_t nbytes );
  327. #endif
  328.  
  329. #if !defined( STDMSC )
  330. extern void near *cdecl _nrelocate( void near *ptr );
  331. #endif
  332.  
  333. extern void      *cdecl realloc( void *ptr, size_t nbytes );
  334.  
  335. #if !defined( STDMSC )
  336. extern void      *cdecl _relocate( void *ptr );
  337. #endif
  338.  
  339. extern void      *cdecl sbrk( int );
  340.  
  341. extern size_t     cdecl stackavail( void );
  342.  
  343. /*/
  344.       Include debug functions if necessary
  345. /*/
  346. #if !defined( STDMSC) && ( defined( _HEAPDEBUG ) || defined( _HEAPTRACE ) )
  347.  
  348.    #include <heapdbg.h>
  349.  
  350. #endif
  351.  
  352. #endif   /* defined( HEAP_H ) */
  353.